package com.jcz.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.jcz.biz.Vote_ItemBiz;
import com.jcz.biz.Vote_OptionBiz;
import com.jcz.biz.Vote_SubjectBiz;
import com.jcz.bizImpl.Vote_ItemBizImpl;
import com.jcz.bizImpl.Vote_OptionBizImpl;
import com.jcz.bizImpl.Vote_SubjectBizImpl;
import com.jcz.entity.Vote_Item;
import com.jcz.entity.Vote_Option;
import com.jcz.entity.Vote_Subject;
import com.jcz.entity.Vote_User;
import com.opensymphony.xwork2.ActionSupport;

public class VoteAction extends ActionSupport {
	private Vote_Subject subject;
	private Integer[] options;
	private Map<Integer, Long> stat=new HashMap<Integer, Long>();/*各项得票数*/
	private Map<Integer, Double> statPercent=new HashMap<Integer,Double>();/*各项所占百分百*/
	private long num;/*subject总票数*/

	
	public long getNum() {
		return num;
	}


	public void setNum(long num) {
		this.num = num;
	}


	public Map<Integer, Long> getStat() {
		return stat;
	}


	public void setStat(Map<Integer, Long> stat) {
		this.stat = stat;
	}


	public Map<Integer, Double> getStatPercent() {
		return statPercent;
	}


	public void setStatPercent(Map<Integer, Double> statPercent) {
		this.statPercent = statPercent;
	}


	public Integer[] getOptions() {
		return options;
	}


	public void setOptions(Integer[] options) {
		this.options = options;
	}


	public Vote_Subject getSubject() {
		return subject;
	}


	public void setSubject(Vote_Subject subject) {
		this.subject = subject;
	}

	Vote_SubjectBiz sBiz=new Vote_SubjectBizImpl();
	Vote_ItemBiz iBiz=new Vote_ItemBizImpl();
	/*第一次进入投票页面时*/
	public String first() throws Exception {
		
		HttpServletRequest request=ServletActionContext.getRequest();
		String id=request.getParameter("subject.id");
		
		this.setSubject(sBiz.findById(Integer.parseInt(id)));
		return "first";
	}
	
	/*提交投票结果*/
	public String save(){
		
		HttpSession session=ServletActionContext.getRequest().getSession();
		Vote_User user=(Vote_User)session.getAttribute("login");
		
		for (Integer integer : options) {
			Vote_Item item=new Vote_Item();
			
			Vote_Option option=new Vote_Option();
			option.setVo_id(integer);
			
			item.setOptions(option);
			item.setUser(user);
			item.setSubject(subject);
			
			iBiz.addItem(item);
		}
		
		return "save";
	}
	
	/*查看投票结果*/
	
	public String view(){
		HttpServletRequest request=ServletActionContext.getRequest();
		String id=request.getParameter("subject.id");
		this.setSubject(sBiz.findById(Integer.parseInt(id)));
		count();
		proportion();
		
		return "view";
	}
	
	/*获取各项投票数*/
	public void count(){
		
		for (Vote_Option option : subject.getOptions()) {
			stat.put(option.getVo_id(), iBiz.findById(option.getVo_id()));
			num+=iBiz.findById(option.getVo_id());
		}
	}
	
	/*计算百分比*/
	public void proportion(){
		for (Vote_Option option : subject.getOptions()) {
			Double xx=stat.get(option.getVo_id())*1.0/num*1.0;
			statPercent.put(option.getVo_id(), xx*100);
			
		}
	}
	
}
